<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 用户信息卡片视图 -->
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <el-row :gutter="5">
        <!-- 输入框 -->
        <el-col :span="10">
          <el-input
            clearable
            placeholder="输入用户账户"
            v-model="queryParams.user_id"
            @clear="getOrdersList()">
            <el-button slot="append" icon="el-icon-search" @click="getOrdersList()"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="ordersList" border style="width: 100%">
        <!-- 索引列 -->
        <el-table-column type="index" label="#" width="50px"></el-table-column>
        <!-- 订单编号列 -->
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <!-- 订单价格列 -->
        <el-table-column prop="order_price" label="订单价格" width="80px"></el-table-column>
        <!-- 是否付款列 -->
        <el-table-column label="是否付款" width="80px">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.order_pay === '1'">已付款</el-tag>
            <el-tag size="mini" type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <!-- 是否发货列 -->
        <el-table-column prop="is_send" label="是否发货" width="80px"></el-table-column>
        <!-- 订单时间列 -->
        <el-table-column label="订单时间" width="160px">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showBox(scope.row)"></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              @click="showProgressBox(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页栏 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryParams.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"></el-pagination>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑地址" :visible.sync="boxVisible" width="50%">
      <!-- 添加分类表单 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef">
        <!-- 联动选择器 -->
        <el-form-item label="省市区/县" prop="dizhi1">
          <el-cascader
            v-model="editForm.dizhi1"
            :options="cityData"
            :props="peizhiProps"
            @change="selectChange"
            clearable></el-cascader>
        </el-form-item>
        <!-- 文本输入框 -->
        <el-form-item label="详细地址" prop="dizhi2">
          <el-input v-model="editForm.dizhi2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="boxVisible = false">取 消</el-button>
        <el-button type="primary" @click="boxVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流对话框 -->
    <el-dialog title="物流信息" :visible.sync="progressBoxVisible" width="50%">
      <el-timeline reverse>
        <el-timeline-item
          v-for="(activity, index) in progress"
          :key="index"
          :class="activity.status === '1' ? itemColor : ''"
          :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressBoxVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressBoxVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '@/components/order/citydata.js'
export default {
  data() {
    return {
      // 订单列表数据
      ordersList: [],
      // 请求参数
      queryParams: {
        query: '',
        pagenum: 1,
        pagesize: 5,
        user_id: '',
        pay_status: '',
        is_send: '',
        order_fapiao_title: '',
        order_fapiao_company: '',
        order_fapiao_content: '',
        consignee_addr: ''
      },
      // 总数
      total: 0,
      // 控制编辑对话框显示隐藏
      boxVisible: false,
      editForm: {
        dizhi1: [],
        dizhi2: ''
      },
      editFormRules: {},
      cityData: cityData,
      peizhiProps: {
        value: 'value',
        label: 'label',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: false
      },
      editFormRules: {
        dizhi1: [{ required: true, message: '请选择地址', trigger: 'blur' }],
        dizhi2: [{ required: true, message: '请填写地址', trigger: 'blur' }]
      },
      progressBoxVisible: false,
      // 物流信息 mock 数据
      progress: [
        {
          time: '2018-05-09 13:07:00',
          ftime: '2018-05-09 13:07:00',
          context: '顺丰速运 已收取快件',
          location: '',
          status: 0
        },
        {
          time: '2018-05-09 12:25:03',
          ftime: '2018-05-09 12:25:03',
          context: '卖家发货',
          location: '',
          status: 1
        },
        {
          time: '2018-05-09 12:22:24',
          ftime: '2018-05-09 12:22:24',
          context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
          location: '',
          status: 1
        },
        {
          time: '2018-05-08 21:36:04',
          ftime: '2018-05-08 21:36:04',
          context: '商品已经下单',
          location: '',
          status: 1
        }
      ]
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryParams })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.ordersList = res.data.goods
      this.total = res.data.total
      // console.log(res.data.goods)
    },
    // 改变每页显示条数
    handleSizeChange(newSize) {
      this.queryParams.pagesize = newSize
      this.getOrdersList()
    },
    // 跳转到哪一页
    handleCurrentChange(val) {
      this.queryParams.pagenum = val
      this.getOrdersList()
    },
    // 显示编辑对话框
    showBox(row) {
      this.boxVisible = true
    },
    // 显示物流信息对话框
    showProgressBox(row) {
      this.progressBoxVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}

.itemColor {
  color: aqua;
}
</style>
